<?php
namespace hangpu8\hpcms\service;

use hangpu8\hpcms\Utils\Data;
use think\Exception;

class Menu{
    /**
     * 获取权限菜单
     *
     * @param array $rules
     * @return array
     */
    public function getMenus(array $rules)
    {
        $dataTree = Data::channelLevel($rules, '', '&nbsp;', 'path', 'pid');
        $trees = $this->getChildren($dataTree);
        return $trees;
    }

    /**
     * 获取无限级菜单
     *
     * @param array $data
     * @return array
     */
    private function getChildren(array $data)
    {
        // 组件类型
        $componentArr = [
            'router'                    => '',
            'component'                 => 'remote/index',
            'form'                      => 'form/index',
            'tabForm'                   => 'form/tabForm',
            'structure'                 => 'form/structure',
            'list'                      => 'table/index',
            'treeTable'                 => 'table/tree',
        ];
        $trees = [];
        $i = 0;
        foreach ($data as $value)
        {
            $component = '';
            if (!isset($componentArr[$value['type']]))
            {
                throw new Exception("{$value['type']} 组件不存在");
            }else if($value['type'] !== 'router')
            {
                $component = rtrim($componentArr[$value['type']]);
            }
            $path = "/{$value['path']}";
            $name = str_replace('/', '_', $value['path']);
            list($module,$controller,$action) = explode('/',$value['path']);
            if ($module == "hangpu8")
            {
                if (empty($value['version'])) {
                    $router = "/{$module}/{$controller}/{$action}";
                }else{
                    $router = "/{$module}/{$value['version']}.{$controller}/{$action}";
                }
            }else{
                if (empty($value['version'])) {
                    $router = "/{$module}/{$controller}/{$action}";
                }else{
                    $router = "/{$module}/{$value['version']}.{$controller}/{$action}";
                }
            }
            $item = [
                'name'              => rtrim($name),
                'title'             => $value['title'],
                'path'              => $path,
                'params'            => [
                    'title'         => $value['title'],
                    'desc'          => $value['desc'],
                    'show'          => $value['show'],
                    'icon'          => $value['icon'],
                    'method'        => $value['method'],
                    'http'          => $router,
                    'component'     => $component,
                    'remote'        => $value['remote']
                ],
            ];
            $trees[$i] = $item;
            if ($value['_data']) {
                $trees[$i]['children'] = $this->getChildren((array)$value['_data']);
            }
            $i++;
        }
        return $trees;
    }
}